window.util = {
  channel: 'H5',    //渠道
  reqKey: '1AD92CD4628A623062AD097EEED1CED9E50FA9782EE294136C0868FA2CEE9241',
  hostUrl: 'https://api.jqy.miaoxiakuan.com/',
  app: 1,
  appName: '借钱用',

  //统计接口
  // tjUrl: 'https://api.data.miaoxiakuan.net/v1/report',
  // tjReqKey: '2BDC04A6FA63F0E4DE230392B5BBE76F425E45E288EB7FEBE4704B2549504F502107FEB102F168B5C4732A1096774AC9',
  /**
  * 页面内容不够时，主动设置页面高度为浏览器高度
  * */
  setPageHeight: function () {
    document.body.style.height = window.innerHeight+'px';
  },

  /**
  * 将nodeList转换为Array
   * args: nodes: nodeList
   * return: array
  * */
  nList2Arr: function (nodes) {
    return Array.prototype.slice.call(nodes, 0);
  },

  /**
  * 显示
  * */
  showBtn: function () {
    this.classList.add('active');
  },

  /**
  * 隐藏
  * */
  hideBtn: function () {
    this.classList.remove('active');
  },

  /**
  * 显示或隐藏输入框的清空按钮
   * args: ipt: 输入框的引用， btn: 清空按钮的引用
  * */
  showOrHideClearBtn: function (ipt, btn) {
    if (ipt.value !== '') {
      this.showBtn.apply(btn);
    } else {
      this.hideBtn.apply(btn);
    }
  },

  /**
  * 清空输入框的值
   * args: ipt: 输入框的引用, btn: 清空按钮的引用
  * */
  clearIpt: function (ipt, btn) {
    ipt.value = '';
    this.hideBtn.apply(btn);
  },

  /**
  * 显示或隐藏密码框的预览按钮
   * args: ipt: 密码框的引用， btn: 预览按钮的引用
  * */
  showOrHidePreviewBtn: function (ipt, btn) {
    if (ipt.value !== '') {
      this.showBtn.apply(btn);
    } else {
      this.hideBtn.apply(btn);
    }
  },

  /**
  * 改变密码的状态为可见或不可见
   * args: ipt: 密码框的引用， btn: 预览按钮的引用
  * */
  changePwdState: function (ipt, btn) {
    if (btn.classList.contains('ipt-eye-open')) {
      btn.classList.remove('ipt-eye-open');
      ipt.setAttribute('type', 'password');
    } else {
      btn.classList.add('ipt-eye-open');
      ipt.setAttribute('type', 'text');
    }
  },

  /**
  * 请求验证码
   * args:
   *  elem : 执行倒计时的按钮,
   *  phone: 手机号,
   *  type: 场景类型 （登录1、改密2、注册3）
  * */
  fetchCode: function (elem, phone, type) {
    elem.classList.add('active');
    var text = elem.innerHTML;
    var count = 59;
    var inter = setInterval(function () {
      if (count > 0) {
        elem.innerHTML = count + 's重新获取';
        count--;
      } else {
        elem.classList.remove('active');
        elem.innerHTML = text;
        clearInterval(inter);
      }
    }, 1000);

    $.ajax({
      url: util.hostUrl + '/v1/userInfo/getCode.html',
      type: 'post',
      data: {
        header: JSON.stringify({
          reqKey: util.reqKey
        }),
        param: JSON.stringify({
          type: type,
          phone: phone,
          appName: util.appName,
          app: util.appName
        })
      },
      success: function (res) {
        if (res.result.code == 1) {
          // do nothing
        } else if (res.result.code == 3) {
          util.toast('该用户还未注册！');
        } else if (res.result.code == 4) {
          util.toast('用户已经注册过！');
        } else {
          util.toast('请稍后获取！');
        }
      },
      error: function () {
        util.toast('Oops,网络有问题！');
      }
    });
  },

  /**
  * 弹出提示框
  * */
  toast: function (title) {
    var toast = document.body.querySelector('.toast');
    if (toast) {
      document.body.removeChild(toast);
    }
    var span = document.createElement('span');
    span.className = 'toast';
    span.innerHTML = title;
    document.body.appendChild(span);

    toast = document.body.querySelector('.toast');
  },

  /**
  * 隐藏手机号中间四位
   * args: phone: 手机号字符串
   * return: 处理之后的手机号字符串
  * */
  hidePhone: function (phone) {
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  },

  /**
  * 获取url中的search值，返回json对象
   * args: search: url中的query string
   * return: 返回解析出来的JSON对象
  * */
  splitUrl: function (search) {
    var temp = [], arr = [], urlMap = {};
    if (search[0] == '?') {
      search = search.substring(1);
    }
    if (search.indexOf('&') !== -1) {
      arr = search.split('&');
      arr.forEach(function (s) {
        temp = s.split('=');
        urlMap[temp[0]] = temp[1];
      });
    } else if (search.indexOf('=') !== -1) {
      arr = search.split('=');
      urlMap[arr[0]] = arr[1];
    }
    return urlMap;
  },

  /**
  * 获取当前地理位置
   * args: 成功的回调函数
  * */
  getLocation: function (showLocation) {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showLocation);
    }
    else {
      console.log('该浏览器不支持获取地理位置');
    }
  },

  /**
  * 验证密码 8-16位的中英文组合
   * args: pwd: 密码字符串
   * return: true验证通过  false验证失败
  * */
  validatePwd: function (pwd) {  ///^[a-zA-Z0-9]{8,16}$/
    return (/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/.test(pwd));
  },

  /**
  * 分页请求时的loading指示
   * @param {object} elem: 要增加loading的元素
  * */
  addPageLoading: function (elem) {
    var loading = document.createElement('div');
    loading.id = 'loadMore';
    loading.className = 'page-loader';
    loading.innerHTML = String()
        + '<div class="loader loader--style1" title="0">'
        + '<svg version="1.1" id="loader-1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"'
        + 'width=".8rem" height=".8rem" viewBox="0 0 40 40" enable-background="new 0 0 40 40" xml:space="preserve">'
        + '<path opacity="0.2" fill="#000" d="M20.201,5.169c-8.254,0-14.946,6.692-14.946,14.946c0,8.255,6.692,14.946,14.946,14.946'
        + 's14.946-6.691,14.946-14.946C35.146,11.861,28.455,5.169,20.201,5.169z M20.201,31.749c-6.425,0-11.634-5.208-11.634-11.634'
        + 'c0-6.425,5.209-11.634,11.634-11.634c6.425,0,11.633,5.209,11.633,11.634C31.834,26.541,26.626,31.749,20.201,31.749z"/>'
        + '<path fill="#000" d="M26.013,10.047l1.654-2.866c-2.198-1.272-4.743-2.012-7.466-2.012h0v3.312h0'
        + 'C22.32,8.481,24.301,9.057,26.013,10.047z">'
        + '<animateTransform attributeType="xml"'
        + 'attributeName="transform"'
        + 'type="rotate"'
        + 'from="0 20 20"'
        + 'to="360 20 20"'
        + 'dur="1.0s"'
        + 'repeatCount="indefinite"/>'
        + '</path>'
        + '</svg>'
        + '</div>';

    elem.appendChild(loading);
  },

  /**
  * 去掉分页加载指示器
  * */
  removePageLoading: function () {
    var loadMore = document.querySelector('#loadMore');
    if (loadMore) {
      loadMore.parentNode.removeChild(loadMore);
    }
  },

  /**
  * 增加加载数据loading
  * */
  addFetchLoading: function () {
    var fetchLoad = document.createElement('div');
    fetchLoad.id = 'fetchLoad';
    fetchLoad.className = 'fetch-loading';
    fetchLoad.innerHTML = String()
      + '<div class="fetch-loader" title="0">'
      + '<svg version="1.1" id="loader-1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"'
      + 'width="1rem" height="1rem" viewBox="0 0 40 40" enable-background="new 0 0 40 40" xml:space="preserve">'
      + '<path opacity="0.2" fill="#000" d="M20.201,5.169c-8.254,0-14.946,6.692-14.946,14.946c0,8.255,6.692,14.946,14.946,14.946'
      + 's14.946-6.691,14.946-14.946C35.146,11.861,28.455,5.169,20.201,5.169z M20.201,31.749c-6.425,0-11.634-5.208-11.634-11.634'
      + 'c0-6.425,5.209-11.634,11.634-11.634c6.425,0,11.633,5.209,11.633,11.634C31.834,26.541,26.626,31.749,20.201,31.749z"/>'
      + '<path fill="#000" d="M26.013,10.047l1.654-2.866c-2.198-1.272-4.743-2.012-7.466-2.012h0v3.312h0'
      + 'C22.32,8.481,24.301,9.057,26.013,10.047z">'
      + '<animateTransform attributeType="xml"'
      + 'attributeName="transform"'
      + 'type="rotate"'
      + 'from="0 20 20"'
      + 'to="360 20 20"'
      + 'dur="1.0s"'
      + 'repeatCount="indefinite"/>'
      + '</path>'
      + '</svg>'
      + '</div>';

    document.body.appendChild(fetchLoad);
  },

  /**
  * 删除加载数据loading
  * */
  removeFetchLoading: function () {
    var fetchLoad = document.querySelector('#fetchLoad');
    if (fetchLoad) {
      fetchLoad.parentNode.removeChild(fetchLoad);
    }
  },

  /**
  * 没有获取到数据的展示
   * @param {object} elem: 存放展示信息的容器
  * */
  fetchNoData: function (elem) {
    var noData = document.createElement('div');
    noData.className = 'no-data';
    noData.innerHTML = '暂没数据喔！';

    if (elem.style.position == 'static') {
      elem.style.position = 'relative';
    }
    elem.appendChild(noData);
  },

  /**
  * 删除没有数据的展示
  * */
  removeNoData: function () {
    var noData = document.querySelector('.no-data');
    if (noData) {
      noData.parentNode.removeChild(noData);
    }
  },

  /**
  * 处理金钱格式
   * @param {string} str: 金钱的字符串值
  * */
  splitMoney: function(str){
    return str.toString().replace(/,/g,'').split('').reverse().join('').replace(/(\d{3})(?=\d)/g, '$1,').split('').reverse().join('');
  },

  /**
  * 上传统计信息
  * */
  uploadTJInfo: function() {
    $.ajax({
      url: 'https://api.data.miaoxiakuan.net/v1/report',
      type: 'post',
      data: {
        reqKey: '2BDC04A6FA63F0E4DE230392B5BBE76F425E45E288EB7FEBE4704B2549504F502107FEB102F168B5C4732A1096774AC9',
        data: localStorage.TJArray
      },
      success: function (data) {
        console.log(data);
      },
      complete: function () {
        localStorage.removeItem('TJArray');
        localStorage.TJArray = JSON.stringify([]);
      }
    });
  }
};
